if (window.nodoin) _nodoin = nodoin;



// global nodoin object : loads external JS scripts dynamically based on the existance of corresponding class name in the DOM
nodoin = {
    info: 'NodoIn Base JavaScript',
    lang: 'es', // change this to an expression that gets the correct language value [en,es,vi,zh]
    log: function () {
        try { console.log.apply('', arguments); } catch (e) { }
    },
    scriptsLoc: '/js/mod/',
    mod: {
        'mod-notifier': {},
        'mod-oauth': {},
        'accordion': {},
        'ddmenu': {},
        'simplemodal': {},
        'carousel': {},
        'datepicker': {},
        'dismissable_alert': {},
        'facebox': {},
        'validate': {},
        'slideshow': {},
        'scroll_table_bdy': {},
        'tabs': {},
        'tablefilter': {},
        'tablesorter': {},
        'tooltip': {},
        'togglelist': {},
        'featuredbox': {},
        'producttechlist': {},
        'validate': {},
        'expandablelists': {},
        'genericlist': {},
        'scombo': {},
        'leftnavigation': {},
        'latestnews': {},
        'ajaxform': {},
        'ajaxnav': {},
        'bingsearch': {},
        'fbiframe': {},
        'traceurl': {},
        'socialcombo': {},
        'brandsprod': {},
        'iframeloader': {},
        'fixheight': {},
        'roundedcorners': {},
        'rssfeed': {},
        'btn': {}
    },
    loaded: [],
    init: function () {

        // remove non-js message from DOM
        $('.enable-js').remove();

        var ua = navigator.userAgent;
        var ie = /MSIE/.test(ua) && ua.replace(/.*?MSIE (.).*/, "$1");
        var ff = /Firefox/.test(ua) && ua.replace(/.*?Firefox\/(.)\.(.).*/, "$1_$2");
        var safari = /Safari/.test(ua) && ua.replace(/.*?Version\/(.).*/, "$1");
        var chrome = /Chrome/.test(ua) && ua.replace(/.*?Chrome\/(.).*/, "$1");
        var opera = /Opera/.test(ua) && ua.replace(/.*?Opera\/(.).*/, "$1");
        document.getElementsByTagName("body")[0].className = "js" + (ie ? " ie ie" + ie : ff ? " ff ff" + ff : chrome ? " chrome chrome" + chrome : safari ? " safari safari" + safari : opera ? " opera opera" + opera : "");

        // preload images so you don't get flickering
        /*(function(){
        var imagesToLoad = [
        'css/images/bg-tooltip-left.png',
        'css/images/bg-tooltip-right.png',
        'css/images/bg-tooltip-image-left.png',
        'css/images/bg-tooltip-image-right.png'
        ];
        $('<div class="no-print" style="position:absolute;left:-9999px;top:-9999px;height:0;width:0;overflow:hidden;"/>').appendTo('body').html('<img src="'+imagesToLoad.join('"/><img src="')+'"/>');
        })();*/

        // accessibility: [plugin] treat spacebar 'keypress' event the same as the mouse 'click' event
        $.fn.klik = function (f) {
            return this.bind('click keypress', function (e) {
                if (!e.keyCode || e.keyCode == 32) {
                    return f.call(this, e);
                }
            });
        };

        // setup and, if needed, load modules & plugins from other files
        $.each(nodoin.mod, function (moduleName) {
            $.extend(nodoin.mod[moduleName], {
                className: '.mod-' + moduleName,
                load: function (m) {
                    if ($.inArray(moduleName, nodoin.loaded) < 0) {

                        var headElem = document.getElementsByTagName('head')[0];
                        var newScript = document.createElement('script');
                        newScript.type = 'text/javascript';
                        newScript.src = nodoin.scriptsLoc + 'nodoin.' + moduleName.replace(/_/g, '-') + '.js';
                        headElem.appendChild(newScript);

                        (function () {
                            if (!nodoin.mod[moduleName].init) {
                                setTimeout(arguments.callee, 30);
                                return;
                            }
                            nodoin.mod[moduleName].init(m);
                        })();

                        nodoin.loaded.push(moduleName);
                    }
                }
            });

            var module = $('.' + moduleName.replace(/_/g, '-'));
            if (module.size()) {
                nodoin.mod[moduleName].load(module);
            }
        });

        // print entire page - using "print-page" class
        $('.print-page').klik(function () {
            window.print();
        });

        $.fn.inputHint = function (overClass) {
            return this.each(function () {
                var hint = $(this);
                // in this scope, "var f" means the element attribute "for"
                // .. "for" is a reserved word in the ECMA grammar
                var f = hint.attr('for') || hint.attr('title').split('for:').pop();
                if (f) {
                    var input = $('#' + f).data('input-hint', hint[0]);

                    this.hide = function () {
                        hint.css({ left: -9999 });
                    };

                    this.show = function () {
                        if (input.val() == '') hint.css({ left: 0 });
                    };

                    // handlers
                    input.focus(this.hide);
                    input.blur(this.show);
                    hint.addClass(overClass).click(function () { input.focus() });

                    if (input.val() != '') this.hide();
                }
            });
        };

        // apply plugin
        $('.input-hint').inputHint('hint-over');

    },
    arr_diff: function (a1, a2) { //this function is for compare two arrays an obtain the difference
        var a = [], diff = []; //just in case we want to know wich modules are not loaded
        for (var i = 0; i < a1.length; i++)
            a[a1[i]] = true;
        for (var i = 0; i < a2.length; i++)
            if (a[a2[i]]) delete a[a2[i]];
            else a[a2[i]] = true;
        for (var k in a)
            diff.push(k);
        return diff;
    },
    reload: function (reload) {//reload is the array of modules to load
        var notloadedMod;
        var loadedMod = [];
        $.each(nodoin.mod, function (moduleName) {
            loadedMod.push(moduleName);
        });
        if (!reload || reload.lenght == 0) {//if we don't pass the array of modules to try to reload
            notloadedMod = nodoin.arr_diff(loadedMod, nodoin.loaded); //the script try to load, the modules that are not loaded yet
        } else {
            notloadedMod = reload; //in the other case it will try to load the modules you put in the array
        }
        //nodoin.log(notloadedMod[0]);
        $.each(notloadedMod, function (index, moduleName) {
            nodoin.log("trying to reload " + moduleName);
            $.extend(nodoin.mod[moduleName], {
                className: '.mod-' + moduleName,
                load: function (m) {
                    var s = document.createElement('script');
                    s.src = nodoin.scriptsLoc + 'nodoin.' + moduleName.replace(/_/g, '-') + '.js';
                    $('head').append(s);
                    (function () {
                        if (!nodoin.mod[moduleName].init) {
                            setTimeout(arguments.callee, 30);
                            return;
                        }
                        nodoin.mod[moduleName].init(m);
                    })();

                    nodoin.loaded.push(moduleName);
                }
            });
            var module = $('.' + moduleName.replace(/_/g, '-'));
            if (module.size()) {
                nodoin.mod[moduleName].load(module);
            }
        });
    },
    someFunctionToCall: function () {

        // some function you want to call by name
    },
    someOtherFunctionToCall: function () {

        // another function you want to call by name
    }
};

// instantiate nodoin object
nodoin.init();
